//
//  JXFeedbackController.m
//  JiaoX
//
//  Created by yinqy on 15/1/20.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXFeedbackController.h"
#import "JXTextView.h"
#import "SelfDataTool.h"

@interface JXFeedbackController ()<UIScrollViewDelegate,UITextViewDelegate,UITextFieldDelegate>
{
    UIScrollView *_contentScroll;
    JXTextView *_textView;
    UITextField *_textField;
    UIButton *_commitBtn;
}

@end

@implementation JXFeedbackController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"意见反馈"];
    [self.view setBackgroundColor:JXGlobalColorBg];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStyleBordered target:self action:@selector(commitBtnClick)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    _contentScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, JXScreenW, self.view.height-NavHeight)];
    [_contentScroll setDelegate:self];
    [_contentScroll setContentSize:CGSizeMake(JXScreenW, _contentScroll.height)];
    [self.view addSubview:_contentScroll];
    
    [self initView];
    
}

-(void)initView{
    _textView = [[JXTextView alloc]initWithFrame:CGRectMake(JXPublicInset, JXPublicInset*3, JXScreenW-JXPublicInset*2, 160)];
    //设置边框
    [_textView.layer setBorderColor:JXColor(204, 204, 204).CGColor];
    [_textView.layer setBorderWidth:0.7];
    [_textView.layer setCornerRadius:5];
    [_textView setPlaceholder:@"想给羞羞说点啥~"];
    [_textView setDelegate:self];
    [_contentScroll addSubview:_textView];
    
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_textView.frame)+JXPublicInset*1.5, _textView.width, 46)];
    [_textField setValue:JXColor(153, 153, 153) forKeyPath:@"_placeholderLabel.textColor"];
    [_textField.layer setBorderColor:JXColor(204, 204, 204).CGColor];
    [_textField.layer setBorderWidth:0.7];
    [_textField setKeyboardType:UIKeyboardTypePhonePad];
    [_textField.layer setCornerRadius:5];
    [_textField setPlaceholder:@"请输入您的手机号"];
    [_textField setDelegate:self];
    [_textField setFont:[UIFont systemFontOfSize:15]];
    //输入框添加左边距
    CGRect frame = [_textField frame];
    frame.size.width = 7.0f;
    UIView *leftview = [[UIView alloc] initWithFrame:frame];
    _textField.leftViewMode = UITextFieldViewModeAlways;
    _textField.leftView = leftview;
    [_contentScroll addSubview:_textField];
    
}

-(void)commitBtnClick{
    if ([Utils isBlankString:_textView.text]) {
        [DialogUtils showTipsWithView:self.navigationController.view String:@"反馈内容不能为空哦～"];
        return;
    }
    if ([Utils isBlankString:_textField.text] || ![Utils isPhoneNumber:_textField.text]) {
        [DialogUtils showTipsWithView:self.navigationController.view String:@"请输入正确的手机号码"];
        return;
    }
    [self.view endEditing:YES];
    MBProgressHUD *HUD = [DialogUtils showLoadingWithView:self.navigationController.view];
    [SelfDataTool commitFeedback:_textField.text contact:_textView.text success:^(JXBaseResponse *result) {
        [HUD hide:YES];
        if ([Utils isBlankString:result.data]) {
            [DialogUtils showTipsWithView:self.navigationController.view String:@"羞羞已收到"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        [HUD hide:YES];
        [DialogUtils showTipsWithView:self.navigationController.view String:@"提交失败"];
    }];
    
}


- (void)textViewDidBeginEditing:(UITextView *)textView{
    [textView.layer setBorderColor:JXThemeColor.CGColor];
}
- (void)textViewDidEndEditing:(UITextView *)textView{
     [textView.layer setBorderColor:JXColor(204, 204, 204).CGColor];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [textField.layer setBorderColor:JXThemeColor.CGColor];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [textField.layer setBorderColor:JXColor(204, 204, 204).CGColor];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}
-(BOOL)shouldLogin{
    return YES;
}

@end
